package com.parking.gateway.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.parking.common.core.model.Result;
import org.springframework.http.MediaType;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class FilterUtil {
    public static Mono<Void> negative(ServerWebExchange exchange, Result result){
        exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
        try {
            String resJson = new ObjectMapper().writeValueAsString(result);
            return exchange.getResponse().writeWith(Mono.fromSupplier(()->{
                return exchange.getResponse().bufferFactory().wrap(resJson.getBytes());
            }));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
